import os
import re
import requests

# Server酱 SendKey，用于发送消息到微信
server_sendkey = "SCT284274TVxefU8r6PhWe6bMdJ4aV8LaA"

def get_disk_usage():
    """获取根分区使用百分比"""
    df_output = os.popen("df -h /").read()
    usage = re.search(r'\d+%',df_output).group()
    return int(usage.replace("%",''))

def send_wechat(message):
    """通过 Server 酱发消息到微信"""
    url = f'https://sctapi.ftqq.com/{server_sendkey}.send'
    data = {
        "title": "azal node3 磁盘告警",
        "desp": message
    }
    response = requests.post(url,data = data)
    if response.status_code == 200:
        print("告警发送成功")
    else:
        print("告警发送失败")

def main():
    usage = get_disk_usage()
    if usage > 85:
        message = f'⚠️ 根分区使用率过高：{usage}% ⚠️\n请及时清理磁盘空间！'
        print(message)
        send_wechat(message)
    else:
        print(f'当前根分区使用率：{usage}%')

if __name__ == '__main__':
    main()